package schema.ext;

import jhunter.service.ServiceInterceptor;
import net.sf.cglib.proxy.Enhancer;
import schema.vo.module.Service;

/**
 * @author jkyang
 */
public class ServiceExt {

	private ModuleExt moduleExt;

	private Service service;

	public ServiceExt(ModuleExt moduleExt, Service service) {
		super();
		this.moduleExt = moduleExt;
		this.service = service;
		initServiceInstance();
	}

	private void initServiceInstance() {
		// TODO
		String clazz = service.getClazz();
		try {
			Class<?> c = moduleExt.getInstanceClassLoader().loadClass(clazz);
			Enhancer enhancer = new Enhancer();
			enhancer.setSuperclass(c);
			enhancer.setCallback(new ServiceInterceptor());
			jhunter.service.Service instance = (jhunter.service.Service) enhancer.create();
			AppExt appExt = (AppExt)moduleExt.getParent();
			instance.setDataSourceTransactionManager(appExt.getAppContext().getDataSourceTransactionManager());
			instance.setDaoFactory(appExt.getAppContext().getDaoFactory());
			serviceInstance = instance;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	private Object serviceInstance;

	public Object getServiceInstance() {
		return serviceInstance;
	}

	public ModuleExt getModuleExt() {
		return moduleExt;
	}

	public void setModuleExt(ModuleExt moduleExt) {
		this.moduleExt = moduleExt;
	}

	public Service getService() {
		return service;
	}

	public void setService(Service service) {
		this.service = service;
	}

}
